package com.lanou3g.farmdoctor.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou3g.farmdoctor.R;
import com.lanou3g.farmdoctor.adapter.AnswerListAdapter;
import com.lanou3g.farmdoctor.adapter.QuestionHeadAdapter;
import com.lanou3g.farmdoctor.base.BaseActivity;
import com.lanou3g.farmdoctor.entity.AnswerBean;
import com.lanou3g.farmdoctor.entity.QandABean;
import com.lanou3g.farmdoctor.volley.VolleySingleton;
import com.lanou3g.farmdoctor.widget.NoScrollGV;
import com.lanou3g.farmdoctor.widget.RoundImageView;

import org.json.JSONObject;

/**
 * Created by ChenFengYao on 15/11/24.
 * 问题详情的Activity
 */
public class QuestionActivity extends BaseActivity implements View.OnClickListener {
    public static QandABean.ListEntity data;//数据

    private TextView back;
    private ListView answerList;
    private View headView;
    private HeadViewHolder headViewHolder;
    private QuestionHeadAdapter headAdapter;
    private AnswerListAdapter answerListAdapter;
    private VolleySingleton volleySingleton;

    
    public static void startQuestionActivity(Context form,QandABean.ListEntity data){
        QuestionActivity.data = data;
        Intent intent = new Intent(form,QuestionActivity.class);
        form.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        initView();//初始化组件
        initData();//初始化数据
    }

    private void initData() {
        volleySingleton = VolleySingleton.getInstance();
        String url = "http://www.nongyisheng.com/question/detail2?qid=" +
                "133241" +
                "&fr=android&vc=33&ver=2.0.5&rn=10&um=_xiaomi&sys=4.4.4";
        Log.d("QuestionActivity", "url:" + url);
        volleySingleton._addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //数据拉取成功
               // Gson gson = new Gson();
               // Log.d("QuestionActivity", response);
                AnswerBean bean = JSON.parseObject(response, AnswerBean.class);
                answerListAdapter.addData(bean.getReplies());
                headViewHolder.answerNum.setText(bean.getReplies().size()+"个回答");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(QuestionActivity.this,
                        "数据拉取失败",Toast.LENGTH_SHORT).show();
            }
        });
    }

    //初始化View
    private void initView() {
        headAdapter = new QuestionHeadAdapter(data.getPicThumbs(),this);//头布局的加载器
        answerListAdapter = new AnswerListAdapter(this);//回答列表的布局加载器

        back = (TextView) findViewById(R.id.tv_question_title_back);
        back.setOnClickListener(this);

        answerList = (ListView) findViewById(R.id.lv_answer);

        headView = LayoutInflater.from(this).inflate(R.layout.head_answer_list,null);
        headViewHolder = new HeadViewHolder(headView);//初始化headView里的组件

        answerList.addHeaderView(headView);//加载头布局
        answerList.setAdapter(answerListAdapter);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_question_title_back:
                finish();//返回
                break;
        }
    }

    //放着头布局的View
    class HeadViewHolder{
        //headView的布局
        public TextView questionTitle;
        public RoundImageView questionHead;
        public TextView timeTv;
        public NoScrollGV questionPicGv;
        public TextView placeTv,answerNum;

        public HeadViewHolder(View head) {
            questionTitle = (TextView) head.findViewById(R.id.tv_head_answer_title);
            questionHead = (RoundImageView) head.findViewById(R.id.iv_icon_question_head);
            timeTv = (TextView) headView.findViewById(R.id.tv_question_head_time);
            questionPicGv = (NoScrollGV) headView.findViewById(R.id.gv_question_head_pic);
            placeTv = (TextView) headView.findViewById(R.id.tv_question_head_place);
            answerNum = (TextView) head.findViewById(R.id.tv_question_list_answer_num);
            initHeadData();
        }

        //初始化头布局的数据
        private void initHeadData(){
            questionTitle.setText(data.getContent());//问题标题
            placeTv.setText(data.getDistrict());//提问地点
            questionPicGv.setAdapter(headAdapter);
            answerNum.setText(String.valueOf(data.getReplycount()));
        }
    }
}
